package com.quanshu.shop.pay;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.quanshu.exchange.R;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.shop.base.tool.BaseTool;
import com.quanshu.shop.base.view.BaseTitleBar;
import com.quanshu.shop.main.api.ShopMainIF;
import com.quanshu.shop.order.SubmitOrderActivity;
import com.quanshu.shop.order.entity.CreatOrderEntity;
import com.quanshu.shop.order.entity.MyBalanceEntity;
import com.quanshu.shop.pay.custom.PayPasswordDialog;
import com.quanshu.shop.pay.view.NjfPayResultActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PayNjfActivity extends BaseActivity {

    @BindView(R.id.pay_njf_title)
    BaseTitleBar payNjfTitle;
    @BindView(R.id.pay_njf_txt1)
    TextView payNjfTxt1;
    @BindView(R.id.pay_njf_img1)
    ImageView payNjfImg1;
    @BindView(R.id.pay_njf_line)
    View payNjfLine;
    @BindView(R.id.pay_njf_txt2)
    TextView payNjfTxt2;
    @BindView(R.id.pay_njf_txt3)
    TextView payNjfTxt3;
    @BindView(R.id.pay_njf_txtOk)
    TextView payNjfTxtOk;
    private Bundle getBundle;
    private CreatOrderEntity creatOrderEntity;
    private PayPasswordDialog payPasswordDialog;
    private String mNjf;
    private Context mContext;
    private Bundle bundle;
    public static final String NJF_PAY_STATE = "njf_pay_state";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_njf);
        ButterKnife.bind(this);
        mContext = this;
        initView();
    }

    private void initView() {
        bundle = getIntent().getExtras();
        payPasswordDialog = new PayPasswordDialog(this).setPayListener(new PayPasswordDialog.OnPayListener() {
            @Override
            public void isSuccessPay(boolean is) {
                if (is) {
                    //支付成功  传入支付成功的参数
                    //跳转到牛积分支付结果页面
                    if (bundle != null) {
                        bundle.putString(NJF_PAY_STATE, "1");
                    }
                    BaseTool.goActivity(mContext, NjfPayResultActivity.class, bundle);
                    finish();
                } else {
                    //支付失败  传入支付失败的参数
                    //跳转到牛积分支付结果页面
                    if (bundle != null) {
                        bundle.putString(NJF_PAY_STATE, "0");
                    }
                    BaseTool.goActivity(mContext, NjfPayResultActivity.class, bundle);
                    finish();
                }
            }
        });
        getMyBalance();
        getBundle = getIntent().getExtras();
        if (getBundle != null) {
            creatOrderEntity = (CreatOrderEntity) getBundle.getSerializable(SubmitOrderActivity.CREATE_ORDER);
        }
        if (creatOrderEntity != null) {
            payNjfTxt2.setText("本次需要" + creatOrderEntity.getNiuScore_sum() + "牛积分兑换");
        }
        payNjfTitle.setLeftImage(R.drawable.icon_back_black_2_0).setCenterText("牛积分订单支付").setOnClickBaseTitleListener(new BaseTitleBar.OnClickBaseTitleBarListener() {
            @Override
            public void onclickLeft() {
                finish();
            }

            @Override
            public void onclickRight() {

            }
        });

        RxViewHelper.getInstance().clicks(payNjfTxtOk, aVoid ->
                {
                    //唤起支付密码弹窗
                    payPasswordDialog.show(creatOrderEntity, mNjf);
                }

        );
    }

    private void getMyBalance() {
        NetClient.getInstance().getShopService(ShopMainIF.class).getMyBalance(currentUser.getId())
                .compose(new BaseTransformer<>(this)).subscribe(new BaseObserver<BaseResponse<MyBalanceEntity>>() {
            @Override
            public void onSuccess(BaseResponse<MyBalanceEntity> response) {
                mNjf = String.valueOf(response.getData().getNiupoint());
                payNjfTxt3.setText("( 余额：" + response.getData().getNiupoint() + " )");
            }
        });
    }
}
